<template>
  <el-container>
    <el-header>
      <h3>学员列表</h3>
    </el-header>
    <el-main>
      <el-form :inline="true" ref="form" :model="formData">
        <el-form-item prop="coach_id" label="所属教练">
          <el-select v-model="formData.coach_id">
            <el-option
              v-for="item in coachList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="degree_id" label="练车进度">
          <el-select v-model="formData.degree_id">
            <el-option
              v-for="item in degreeList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="type_id" label="学员类型">
          <el-select v-model="formData.type_id">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="school_id" label="所属分校">
          <el-select v-model="formData.school_id">
            <el-option
              v-for="item in schoolList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="query_field">
          <el-input v-model.trim="formData.query_field" type="text" suffix-icon="el-icon-search" placeholder="输入学员手机号或者姓名" @keyup.enter.native="search"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.prevent="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click.prevent="reset('form')" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        border
        v-loading="loading"
      >
        <el-table-column
          label="图像"
        >
          <template slot-scope="scope">
            <el-avatar :size="50" :src="scope.row.avatar"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          label="姓名"
          prop="name"
        >
        </el-table-column>
        <el-table-column
          label="手机号"
          prop="mobile"
        >
        </el-table-column>
        <el-table-column
          label="所属分校"
          prop="school_name"
        >
        </el-table-column>
        <el-table-column
          label="所属教练"
          prop="coach"
        >
        </el-table-column>
        <el-table-column
          label="驾考进度"
          prop="degree"
        >
        </el-table-column>
        <el-table-column
          label="学员类型"
          prop="type"
        >
        </el-table-column>
        <el-table-column
          label="约车权限"
        >
          <template slot-scope="scope">
            <el-switch v-model="scope.row.auth" @change="changeAuth(scope.row.id,scope.$index)" disabled></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="推荐学员(个)"
          prop="recommend_student_count"
        >
        </el-table-column>
        <el-table-column
          label="练车记录"
        >
          <template slot-scope="scope">
            <el-link @click.prevent="jumpRecord(scope.row.id)">练车记录</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="推荐学员"
        >
          <template slot-scope="scope">
            <el-link @click.prevent="jumpRecommend(scope.row.id)">查看</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="primary" size="small"  class="el-icon-edit" @click.prevent="info(scope.row.id)">详情</el-button>
            <el-button type="danger" size="small" class="el-icon-delete" @click.prevent="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next, total"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        @current-change="changePage"
      ></el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import {getCoachList, getAllSchoolList} from '@/api/coach'
  import {searchStudentList, delNoApplyStudent} from '@/api/student'
    export default {
        name: "studentList",
        inject: ['reload'],
        data () {
            return {
                formData: {
                    coach_id: 0,
                    degree_id: 0,
                    type_id: 0,
                    query_field: '',
                    school_id: 0,
                },
                coachList: [
                    {id: 0, name: '全部'}
                ],
                degreeList: [
                    {id: 0, name: '全部'},
                    {id: 1, name: '科目一'},
                    {id: 2, name: '科目二'},
                    {id: 3, name: '科目三'},
                    {id: 4, name: '科目四'},
                ],
                typeList: [
                    {id: 0, name: '全部'},
                    {id: 1, name: '普通会员'},
                    {id: 2, name: 'VIP学员'}
                ],
                schoolList: [
                    {id: 0, name: '全部'},
                ],
                tableData: [],
                loading: true,
                total: 0,
                page: 1,
                pageSize: 10
            };
        },
        methods: {
            search () {
              var params ={
                  coach_id: this.formData.coach_id,
                  degree_id: this.formData.degree_id,
                  school_id: this.formData.school_id,
                  type_id: this.formData.type_id,
              };

              if (this.formData.query_field.length != 0) {
                  params.query_field = this.formData.query_field;
              }

              this.loading = true;
              searchStudentList(params).then(response => {
                  this.tableData = response.data.list;
                  this.loading = false;
                  this.total = Number(response.data.paginate.total);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.page = Number(response.data.paginate.current);
              }).catch( err => {
                  this.loading = false;
              })
            },
            reset (ref) {
                this.$refs[ref].resetFields();
            },
            changePage (current) {
                var params = {
                    coach_id: this.formData.coach_id,
                    degree_id: this.formData.degree_id,
                    school_id: this.formData.school_id,
                    type_id: this.formData.type_id,
                    page: current,
                    pageSize: this.pageSize
                };
                if (this.formData.query_field.length != 0) {
                    params.query_field = this.formData.query_field;
                }
                this.loading = true;
                searchStudentList(params).then(response => {
                    this.tableData = response.data.list;
                    this.loading = false;
                    this.total = Number(response.data.paginate.total);
                    this.pageSize = Number(response.data.paginate.pageSize);
                    this.page = Number(response.data.paginate.current);
                    this.loading = false;
                }).catch( err => {
                    this.loading = false;
                })
            },
            jumpRecord(student_id) {
                this.$router.push({path: '/student/record',query: {student_id: student_id}});
            },
            jumpRecommend (student_id) {
                this.$router.push({path: '/student/recommend',query: {student_id: student_id}});
            },
            info (student_id) {
                this.$router.push({path:'/student/info',query:{student_id: student_id}});
            },

            del (student_id) {
                var data = {
                    student_id: student_id
                };
                delNoApplyStudent(data).then(response => {
                    this.$message.success(response.msg);
                    this.reload();
                }).catch( err => {

                })
            }
        },
        mounted() {
            getCoachList().then(response => {
              this.coachList = this.coachList.concat(response.data);
              getAllSchoolList().then(response => {
                  this.schoolList = this.schoolList.concat(response.data);
              })
              searchStudentList({}).then(response => {
                  this.tableData = response.data.list;
                  this.loading = false;
                  this.total = Number(response.data.paginate.total);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.page = Number(response.data.paginate.current);
              })
            }).catch(err => {

            })
        }
    }
</script>

<style scoped>
  .el-container {
    width: 95%;
    max-width: 100%;
    position: relative;
    overflow: hidden;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }

  .el-header{
    text-align: center;
  }
  .el-header h3{
    font-size: 20px;
    height: 20px;
    line-height: 20px;
  }
  .el-footer {
    text-align: center;
  }
</style>
